package com.xhwl.centiotdata.handler.device.impl;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.handler.device.DeviceIntegrationFactory;
import com.xhwl.centiotdata.handler.device.DeviceIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.device.DeviceIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.haikangyunmou.HaiKangYunMouDevice;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.suppliersystem.haikangyunmou.DeviceHaiKangYunMouHandler;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.enums.device.DeviceOnlineStatusEnum;
import com.xhwl.common.pojo.cent.device.Device;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author： lianghz
 * @Description： 海康云眸
 * @Date 2021/09/14/ 09:29
 */
@Service
public class DeviceIntegrationHaiKangYunMouHandler extends DeviceIntegrationHandler {
    private static final Logger log = LoggerFactory.getLogger(DeviceIntegrationHaiKangYunMouHandler.class);

    @Autowired
    private DeviceHaiKangYunMouHandler deviceHaiKangYunMouHandler;
    @Autowired
    private IDeviceCentService deviceCentService;

    /**
     * 海康云眸
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        DeviceIntegrationFactory.register(20, this);
    }

    @Override
    public void pullDevice(DeviceIntegrationConfigDTO configDTO) {
        switch (configDTO.getDeviceType()) {
            case 1:
                this.pullDoor(configDTO);
                break;
        }
    }

    @Override
    public void pullDeviceOnlineStatus(DeviceIntegrationConfigDTO configDTO) {
        switch (configDTO.getDeviceType()) {
            case 1:
                this.pullDoorOnlineStatus(configDTO);
                break;
        }
    }

    /**
     * 拉取门禁设备
     * @param configDTO
     */
    private void pullDoor(DeviceIntegrationConfigDTO configDTO) {
        // 拉取数据
        List<HaiKangYunMouDevice> devices = deviceHaiKangYunMouHandler.pullDoor(configDTO);
        if (CollectionUtils.isEmpty(devices)) {
            return;
        }
        // 转换数据
        List<DeviceDTO> deviceList = this.handleDevice(configDTO, devices);
        // 存储业务中台
        deviceCentService.asyncUpdateDevice(deviceList);
        log.info("拉取海康云眸设备成功：{}", JSONObject.toJSONString(deviceList));
    }

    public void pullDoorOnlineStatus(DeviceIntegrationConfigDTO configDTO) {
        // 拉取数据
        List<HaiKangYunMouDevice> devices = deviceHaiKangYunMouHandler.pullDoorOnlineStatus(configDTO);
        if (CollectionUtils.isEmpty(devices)) {
            return;
        }
        // 转换数据
        List<Device> deviceList = this.handleDeviceOnlineStatus(configDTO, devices);
        // 存储业务中台
        deviceCentService.asyncUpdateOnlineStatus(deviceList);
        log.info("拉取海康云眸设备状态成功：{}", JSONObject.toJSONString(deviceList));
    }

    private List<DeviceDTO> handleDevice(DeviceIntegrationConfigDTO configDTO, List<HaiKangYunMouDevice> devices) {
        return devices.stream().map(d -> {
            DeviceDTO deviceDTO = new DeviceDTO();
            deviceDTO.setProjectId(configDTO.getProjectId());
            deviceDTO.setProductId(configDTO.getProductId());
            deviceDTO.setCategoryId(configDTO.getCategoryEnum().getId());
            deviceDTO.setDeviceTypeId(configDTO.getDeviceType());
            deviceDTO.setName(d.getDeviceName());
            deviceDTO.setThirdDeviceId(d.getDeviceId());
            deviceDTO.setAttributeJson(new JSONObject());
            return deviceDTO;
        }).collect(Collectors.toList());
    }

    private List<Device> handleDeviceOnlineStatus(DeviceIntegrationConfigDTO configDTO, List<HaiKangYunMouDevice> devices) {
        // 0：离线，1：在线
        return devices.stream().map(d -> {
            Device device = new Device();
            device.setProjectId(configDTO.getProjectId());
            device.setThirdDeviceId(d.getDeviceId());
            device.setDeviceTypeId(configDTO.getDeviceType());
            device.setOnlineStatus(d.getDeviceStatus() == 0 ? DeviceOnlineStatusEnum.OFFLINE : DeviceOnlineStatusEnum.ONLINE);
            return device;
        }).collect(Collectors.toList());
    }

}
